<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../../include.inc.jsp"%>
<div class="row">
    <div class="col-lg-12">
        <form id="formDlg" class="form-horizontal" role="form">
            <input type="hidden" id="id" name="id" value="${region.id}" /> 
            <input type="hidden" id="level" name="level" value="${region.level}" /> 
            <input type="hidden" id="pId" name="parent.id" value="${region.parent.id}" />
            <div class="form-group">
                <label class="col-lg-2 control-label">上级机构:</label>
                <div class="col-lg-9">
                    <span class="block input-icon input-icon-right"> 
                        <input readonly type="text" class="form-control" id="parentName"
                            name="parent.name" value="${region.parent.name}" /> 
                        <i class="ace-icon fa fa-cogs"></i>
                    </span>
                    <div class="selTree">
                        <div id="selectTree"></div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">名称:</label> <span
                    class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="text" class="form-control" id="name" name="name" value="${region.name}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">全拼:</label> <span
                    class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="text" class="form-control" id="pinyin" name="pinyin" value="${region.pinyin}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">区域编码:</label> <span
                    class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="text" class="form-control" id="code" name="code" value="${region.code}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">区域长编码:</label> <span
                    class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="text" class="form-control" id="longcode" name="longcode" value="${region.code}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">优先级:</label> <span class="text-danger">*</span>
                <div class="col-lg-9">
                    <input type="number" class="form-control" id="priority" name="priority" value="${region.priority}" />
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    var ajaxTreeUrl = contextPath + '/manage/region/tree';
    
    function hideTree() {
        $(".selTree").fadeOut("fast");
        $(".modal-dialog").unbind("mousedown", onBodyDown);
    }

    function onBodyDown(event) {
        if (!($(event.target).hasClass('selTree') || $(event.target).parents(".selTree").length > 0)) {
            hideTree();
        }
    }
    jQuery(function($) {
        // 初始化树选中
        var id = $('#id').val();
        if (id == "") {
            var $JsTree = $.jstree.reference("treeview");
            var nodes = $JsTree.get_selected(true);
            if (nodes != null && nodes.length > 0) {
                $("#pId").val(nodes[0].id);
                $("#parentName").val(nodes[0].text);
                $("#level").val(parseInt(nodes[0].original.level) + 1);
            }
        }

        $('#parentName').on('click', function() {
            var selectObj = $("#parentName");
            $(".selTree").css({ width : selectObj.outerWidth() + "px" }).slideDown("fast");
            $(".modal-dialog").bind("mousedown", onBodyDown);
        });

        initTree("selectTree", ajaxTreeUrl, false, {
            selNode : function(node, elem) {
                $("#pId").val(node.id);
                $("#parentName").val(node.text);
                $("#level").val(parseInt(node.original.level) + 1);
                hideTree();
            }
        });

        $('#formDlg').validate({
            errorElement : 'span',
            errorClass : 'help-block',
            focusCleanup : false,
            focusInvalid : false,
            onsubmit : false,
            rules : {
                "name":{required:true},
                "priority":{required:true},
                "pinyin":{required:true}
            },
            messages : {
                "name" : '请输入名称',
                "priority" : '请输入优先级',
                "pinyin" : '请输入全拼',
            },
            highlight : function(e) {
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },
            success : function(e) {
                $(e).closest('.form-group').removeClass('has-error').addClass('has-success');
                $(e).remove();
            },
        });
    });
</script>